package alg20241105;

public class Text {
    public int numDecodings(String ss) {
        int n = ss.length();
        int[] s = new int[n];
        for (int i = 0; i < ss.length(); i++) {
            s[i] = Integer.parseInt(ss.substring(i,i+1));
        }
        //1. 创建表
        int[] dp = new int[n];
        //2.初始化
        if(s[0] >= 1 && s[0] <=9) dp[0]++;
        if(dp[0] == 1 && s[1] >= 1 && s[1] <=9) dp[1]++;
        int sum = s[0] * 10 + s[1];
        if(sum >= 10 && sum <= 26) dp[1]++;

        //3.填表
        for (int i = 2; i < n; i++) {
            if(s[i] >= 1 && s[i] <= 9) dp[i] += dp[i-1];
            sum = s[i-1] * 10 + s[i];
            if(sum >= 10 && sum <= 26) dp[i] += dp[i-2];
        }
        //4.返回值
        return dp[n-1];
    }
}
